/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "StdAfx.h"
#include ".\phantomlistbox.h"

CPhantomListBox::CPhantomListBox(void) : CPhantomWindow(String("ListBox"))
{
}

CPhantomListBox::~CPhantomListBox(void)
{
}

int CPhantomListBox::FunctionProcessor(SSI_FunctionAPI& theAPI)
{
	CListBox* List = (CListBox*)aWnd;
	
	if(theAPI.m_FunctionName == "GetCount"){
		SetActive();
		CDataInt i;
		i.Data = (SSI_INT64)List->GetCount();
		theAPI.m_ReturnStatus = theAPI.SetReturnData(&i);

	}else if(theAPI.m_FunctionName == "SelectString"){
		CDataString s;
		s.Data = SSI_EXTRACT(CDataString, 0);
		CDataBool b;
		if(List->SelectString(0, s.Data.Str()) == LB_ERR){
			b.Data = 0;	
		}else{
			b.Data = 1;
		}
		theAPI.m_ReturnStatus = theAPI.SetReturnData(&b);
	}else if(theAPI.m_FunctionName == "Select"){
		SSI_INT64 i = SSI_EXTRACT(CDataInt, 0);
		if(List->GetStyle() & LBS_MULTICOLUMN){
			CString Test;
			List->GetText(i, Test);

			if(List->SelectString(i, (LPCTSTR)Test) == LB_ERR){
				//Error.Format(ERR00160,  StrToInt(InParams), List->GetCount());
				//LogError(Error);
				//return FALSE;
				P_MAKEERR2(ERR_P01530, i, List->GetCount());
				return 0;
			}
		}else if(List->GetStyle() & LBS_MULTIPLESEL){
			if(List->SetSel(i, TRUE) == LB_ERR){
				//Error.Format(ERR00160,  StrToInt(InParams), List->GetCount());
				//LogError(Error);
				//return FALSE;
				P_MAKEERR2(ERR_P01531, i, List->GetCount());
				return 0;
			}
		}else{
			if(List->SetCurSel(i) == LB_ERR){
				//Error.Format(ERR00160,  StrToInt(InParams), List->GetCount());
				//LogError(Error);
				//return FALSE;
				P_MAKEERR2(ERR_P01532, i, List->GetCount());
				return 0;
			}
		}
		theAPI.m_ReturnStatus = STAT_OK;
	}else if(theAPI.m_FunctionName == "GetElement"){
		SSI_INT64 i = SSI_EXTRACT(CDataInt, 0);
		CDataString s;
		CString Test;

    int n = List->GetTextLen( i );
		if(n == LB_ERR){
			P_MAKEERR2(ERR_P01533, i, List->GetCount());
			return 0;
		}

		if(List->GetText( i, Test.GetBuffer(n)) == LB_ERR){
			Test.ReleaseBuffer();
			P_MAKEERR2(ERR_P01533, i, List->GetCount());
			return 0;
		}
    Test.ReleaseBuffer();

		s.Data = (LPCTSTR)Test;
		theAPI.m_ReturnStatus = theAPI.SetReturnData(&s);
	}else if(theAPI.m_FunctionName == "GetSel"){
		CDataInt i;
		i.Data = List->GetCurSel();
		theAPI.m_ReturnStatus = theAPI.SetReturnData(&i);
	}else{
		return CPhantomWindow::FunctionProcessor(theAPI);
	}
	return 1;
}

CPhantomWindow* CPhantomListBox::MakeWindow(void)
{
	return new CPhantomListBox();
}

